package com.example.tnot.Teacher.Homepart.InternalFragment.Frequent.Agent;

import android.annotation.SuppressLint;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.navigation.NavController;
import androidx.viewpager2.widget.ViewPager2;

import com.example.tnot.R;
import com.example.tnot.Teacher.BaseFragment.BaseBackFragment;
import com.example.tnot.Teacher.Homepart.InternalFragment.Frequent.Agent.Adapter.vpAgent;
import com.example.tnot.Teacher.MinePart.InternalFragment.collection.adapter.vpCollectAdapter;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class agentFragment extends BaseBackFragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    View view;
    ImageView back;
    TabLayout tl_agent;
    ViewPager2 vp_agent;
    AgentInternalFragment currentFragment;
    RadioButton rb_in_all,rb_in_computer,rb_in_math,rb_in_sing;
    private RadioGroup rgCourseType;
    private String currentCourseType = "全部";

    String selectedContent = "全部";

    private List<RadioButton> RadioButtonList;

    private String mParam1;
    private String mParam2;

    public agentFragment() {
    }

    public static agentFragment newInstance(String param1, String param2) {
        agentFragment fragment = new agentFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.home_agent_fragment, container, false);
        InitData();
        InitViews();
        setListeners();
        // 创建ViewPager2的适配器
        vpAgent adapter = new vpAgent(getActivity());
        vp_agent.setAdapter(adapter);
        InitCurrentFragment();

        tl_agent.setTabRippleColor(ColorStateList.valueOf(Color.TRANSPARENT));



        // 将TabLayout与ViewPager2联动
        new TabLayoutMediator(tl_agent, vp_agent,
                (tab, position) -> tab.setText(adapter.tabs[position])
        ).attach();



        return view;
    }

    private void InitCurrentFragment() {
        vp_agent.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                currentFragment = getCurrentAgentInternalFragment();
            }
        });
    }


    private void InitData() {

    }

    private void InitViews() {
        back = view.findViewById(R.id.back);
        tl_agent = view.findViewById(R.id.tl_agent);
        vp_agent = view.findViewById(R.id.vp_agent);
        rb_in_all = view.findViewById(R.id.rb_in_all);
        rb_in_computer = view.findViewById(R.id.rb_in_computer);
        rb_in_math = view.findViewById(R.id.rb_in_math);
        rb_in_sing = view.findViewById(R.id.rb_in_sing);

        rgCourseType = view.findViewById(R.id.rgCourseType);
    }

    private AgentInternalFragment getCurrentAgentInternalFragment() {
        // 使用 ViewPager2 的当前项
        int currentPosition = vp_agent.getCurrentItem();

        // 通过适配器获取 Fragment
        vpAgent adapter = (vpAgent) vp_agent.getAdapter();
        if (adapter != null) {
            return adapter.getFragmentAtPosition(currentPosition);
        }
        return null;
    }

    private void setListeners() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                handleBackPressed();

            }
        });
        rb_in_all.setOnClickListener(new View.OnClickListener() {
            @SuppressLint({"UseCompatLoadingForDrawables"})
            @Override
            public void onClick(View view) {
                updateRadioButtonStyle(rb_in_all);
                currentCourseType = "全部";
                currentFragment.updateRecyclerView();
            }
        });

        rb_in_computer.setOnClickListener(new View.OnClickListener() {
            @SuppressLint({"UseCompatLoadingForDrawables"})
            @Override
            public void onClick(View view) {
                updateRadioButtonStyle(rb_in_computer);
                currentCourseType = "计算机";
                currentFragment.updateRecyclerView();
            }
        });

        rb_in_math.setOnClickListener(new View.OnClickListener() {
            @SuppressLint({"UseCompatLoadingForDrawables"})
            @Override
            public void onClick(View view) {
                updateRadioButtonStyle(rb_in_math);
                currentCourseType = "数学";
                currentFragment.updateRecyclerView();
            }
        });

        rb_in_sing.setOnClickListener(new View.OnClickListener() {
            @SuppressLint({"UseCompatLoadingForDrawables"})
            @Override
            public void onClick(View view) {
                updateRadioButtonStyle(rb_in_sing);
                currentCourseType = "合唱";
                currentFragment.updateRecyclerView();
            }
        });
// 在 TabLayout 的监听器中调用
        tl_agent.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                selectedContent = tab.getText().toString();
                if (currentFragment != null) {
                    currentFragment.updateFilter(selectedContent,currentCourseType);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {}

            @Override
            public void onTabReselected(TabLayout.Tab tab) {}
        });

        rgCourseType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb_in_all) {
                    currentCourseType = "全部";
                } else if (checkedId == R.id.rb_in_computer) {
                    currentCourseType = "计算机";
                } else if (checkedId == R.id.rb_in_math) {
                    currentCourseType = "数学";
                } else if (checkedId == R.id.rb_in_sing) {
                    currentCourseType = "合唱";
                }
                // 更新当前 ViewPager 中的 Fragment
                updateCurrentFragment();
            }
        });

    }

    private void updateCurrentFragment() {
        // 获取当前 ViewPager 的 Fragment
        AgentInternalFragment currentFragment = getCurrentAgentInternalFragment();
        Log.d("currentFragment", String.valueOf(currentFragment));
        if (currentFragment != null) {
            currentFragment.updateFilter(selectedContent, currentCourseType);
        }
    }

    private void updateRadioButtonStyle(RadioButton selectedRadioButton) {
        RadioButtonList = new ArrayList<>(Arrays.asList(rb_in_all, rb_in_computer, rb_in_math, rb_in_sing));
        selectedRadioButton.setBackground(getResources().getDrawable(R.drawable.select_border));
        selectedRadioButton.setTextColor(0xFFFFFFFF);

        for (RadioButton radioButton : RadioButtonList) {
            if (radioButton != selectedRadioButton) {
                radioButton.setBackground(getResources().getDrawable(R.drawable.non_select_border));
                radioButton.setTextColor(0xFF999999);
            }
        }
    }
}